package co.edu.unal.satacredito.queue;

import co.edu.unal.satacredito.queue.entity.TransactionPojo;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.jms.*;
import javax.naming.InitialContext;
import javax.naming.NamingException;

@Stateless
public class QueueWriter {

    @Resource(mappedName = "ConnectionFactory")
    private static QueueConnectionFactory queueConnectionFactory;
    
    @Resource(mappedName = "jms/Queue")
    private static Queue queue;
    
    private static QueueConnection queueCon = null;
    private static QueueSender sender = null;
    private static QueueSession session = null;

    public QueueSender OpenQueueConnection() throws IOException, NamingException {
        try {
            InitialContext jndiContext = new InitialContext();
            
            queueConnectionFactory = (QueueConnectionFactory)jndiContext.lookup("ConnectionFactory");
            queue = (Queue)jndiContext.lookup("jms/Queue");
            
            queueCon = queueConnectionFactory.createQueueConnection();
            session = queueCon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
            sender = session.createSender(queue);
            return sender;
        } catch (JMSException ex) {
            Logger.getLogger(QueueWriter.class.getName()).log(Level.SEVERE, null, ex);
            return null;        
        }
    }

    public void SendToQueue(QueueSender sender, TransactionPojo transaction) throws IOException {
        try {
            ObjectMessage omsg = session.createObjectMessage(transaction);
            
//            MapMessage mmsg = session.createMapMessage();
//            mmsg.setString(TransactionPropertiesConstants.BUYER_CARD_NUMBER, transaction.getCardNumber());
//            mmsg.setInt(TransactionPropertiesConstants.BUYER_CARD_TYPE, transaction.getCardType());
//            mmsg.setString(TransactionPropertiesConstants.FILE_ID, transaction.getFileId().toString());
//            mmsg.setString(TransactionPropertiesConstants.FRANCHISE, transaction.getFranchise().toString());
//            mmsg.setString(TransactionPropertiesConstants.BUYER_LASTNAME, transaction.getLastname());
//            mmsg.setString(TransactionPropertiesConstants.BUYER_MIDDLENAME, transaction.getMiddlename());
//            mmsg.setString(TransactionPropertiesConstants.BUYER_NAME, transaction.getName());
//            mmsg.setString(TransactionPropertiesConstants.BUYER_IDENTIFICATION_NUMBER, transaction.getNin());
//            mmsg.setInt(TransactionPropertiesConstants.BUYER_NIN_TYPE, transaction.getNinType());
//            mmsg.setLong(TransactionPropertiesConstants.PURCHASE_PRICE, transaction.getPurchaseAmount());
//            mmsg.setLong(TransactionPropertiesConstants.PURCHASE_DATE, transaction.getPurchaseDate());
//            mmsg.setString(TransactionPropertiesConstants.PURCHASE_DETAILS, transaction.getPurchaseDetail());
            Message msg  = omsg;    
            //System.out.println("QueueWriter : "+(TransactionPojo)omsg.getObject());
            sender.send(msg);
            
            
           // System.out.println("QueueWriter: Sending to Queue");
            
        } catch (JMSException ex) {
            Logger.getLogger(QueueWriter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public boolean CloseQueueConnection() {
        if (queueCon != null) {
            try {
                queueCon.close();
                return true;
            } catch (JMSException ex) {
                Logger.getLogger(QueueWriter.class.getName()).log(Level.SEVERE, null, ex);
                return false;
            }
        }
        return false;
    }
}
